package com.ocom.xcxapi.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.constant.WxServiceUrl;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.xcx.XcxUserInfoVo;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.PersonVo;
import com.ocom.xcxapi.client.HumanClientService;
import com.ocom.xcxapi.client.MinioClientService;
import com.ocom.xcxapi.client.MqSimpleClientService;
import com.ocom.xcxapi.client.PayConfigClientService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Description: TODO
 * @Author: 朱晖
 * @Date: 2022/7/14
 **/
@Slf4j
@RestController
@PreAuthorize("hasRole('ROLE_XCX')")
public class UserController {


	@Autowired
	HumanClientService humanClientService;

	@Autowired
	MinioClientService minioClientService;

	@Autowired
	PayConfigClientService payConfigClientService;

	@Autowired
	MqSimpleClientService mqSimpleClientService;

	 //公众号支付（/pay）
	@PostMapping(WxServiceUrl.URL_UserInfo)
	public Result UserInfo(@RequestBody XcxUserInfoVo xcxUserInfoVo){
		return humanClientService.getXfUserInfo(xcxUserInfoVo.getComId(),xcxUserInfoVo.getPId());
	}


	//修改头像
	@PostMapping(WxServiceUrl.URL_modifyPhoto)
	public Result modifyPhoho(MultipartFile file,  Long comId , Long pId){

		Result result = minioClientService.upload(file);
		if(result.getCode()!=200){
			return ResultUtil.error(ResultEnum.Err_3058);
		}
		JSONObject jsonObject = JSON.parseObject(JSON.toJSONString( result.getData()));

		PersonVo personVo=new PersonVo();
		personVo.setComId(comId);
		personVo.setPId(pId);
		personVo.setPPhotoUrl2(jsonObject.getString("imageUrl"));

		Result result1 = humanClientService.modifyPhoto(personVo);

		log.error("更新人员人脸信息 ZM7FQqBF 【{}】【{}】",personVo,result1);
		if(result1.getCode()!=200){
			return ResultUtil.error(ResultEnum.Err_3014);
		}
//		FaceCheckRequest faceCheckRequest = new FaceCheckRequest();
//		faceCheckRequest.setUrl(jsonObject.getString("imageUrl"));
//		faceCheckRequest.setComId(comId);
//		faceCheckRequest.setPId(pId);
//		mqSimpleClientService.faceCheck(faceCheckRequest);

		return ResultUtil.success(jsonObject.getString("imageUrl"));
	}




}
